// Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "shell/browser/electron_browser_main_parts.h"

#include "base/command_line.h"
#include "base/environment.h"
#include "ui/ozone/public/ozone_switches.h"


constexpr std::string_view kElectronOzonePlatformHint{
    "ELECTRON_OZONE_PLATFORM_HINT"};

namespace electron {

namespace {

// Evaluates the environment and returns the effective platform name for the
// given |ozone_platform_hint|.
// See https://crbug.com/1246928.
std::string MaybeFixPlatformName(const std::string& ozone_platform_hint) {
  LOG(ERROR) << __FUNCTION__ << "You need to implement this function!!!";
  return ozone_platform_hint;
}

}  // namespace

void ElectronBrowserMainParts::DetectOzonePlatform() {
  auto const env = base::Environment::Create();
  auto* const command_line = base::CommandLine::ForCurrentProcess();
  if (!command_line->HasSwitch(switches::kOzonePlatform)) {
    auto ozone_platform_hint =
        command_line->GetSwitchValueASCII(switches::kOzonePlatformHint);
    if (ozone_platform_hint.empty()) {
      env->GetVar(kElectronOzonePlatformHint, &ozone_platform_hint);
    }
    if (!ozone_platform_hint.empty()) {
      command_line->AppendSwitchASCII(
          switches::kOzonePlatform, MaybeFixPlatformName(ozone_platform_hint));
    }
  }

  std::string desktop_startup_id;
  if (env->GetVar("DESKTOP_STARTUP_ID", &desktop_startup_id))
    command_line->AppendSwitchASCII("desktop-startup-id", desktop_startup_id);
}

}  // namespace electron
